package util.regex;

import org.junit.Assert;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则
 */
public class RegexAll {

    /**
     * \\ java 特有 代表 就是 \
     * {} 匹配次数 范围
     * [] 数据内容
     * ^ 开头
     * ()
     */
    public static void main(String[] args) {

        //2个字符
        System.out.println(Pattern.matches("\\w{1,2}", "a3"));

        //2-7个汉字
        System.out.println(Pattern.matches("[\\u4E00-\\u9FA5]{2,7}", "一二三四五六七"));

        //电话号码 1开头 11位数字
        System.out.println(Pattern.matches("^1[\\d]{10}", "15123403091"));

        //0到120
        System.out.println(Pattern.matches("^([0-9]|[1-9]\\d|1[01]\\d|120)$", "9"));

        //18 到 120
        System.out.println(Pattern.matches("^(18|19|[2-9]\\d|1[01]\\d|120)$", "17"));

        //最多两位小数
        //

    }

    @Test
    public void test1() {
        //男女
        Assert.assertTrue(Pattern.matches("[男女]", "男"));

        Assert.assertTrue(Pattern.matches("^(\\s&&[^\\f\\n\\r\\t\\v])*|[男女]$", "男"));

        Assert.assertTrue(Pattern.matches("[12]","2"));//只能匹配其中一个
    }


    @Test
    public void testxxx(){
        Pattern pattern = Pattern.compile("/(.*?)/");                       // 匹配的模式
        Matcher m = pattern.matcher("/NDA/list?a=1");

        while(m.find()){
            System.out.println(m.group(1));
        }


        Pattern pa = Pattern.compile("!(.*?)!");                       // 匹配的模式
        Matcher m1 = pa.matcher("!aaaaa!");

        while(m1.find()){
            System.out.println(m1.group(1));
        }
    }

    @Test
    public void  test3() {
        Assert.assertTrue(Pattern.matches("$|1|2", ""));
        Assert.assertTrue(Pattern.matches("$|1|2", "1"));
        Assert.assertTrue(Pattern.matches("$|1|2", "2"));
        Assert.assertTrue(Pattern.matches("$|1|2", "3"));
//        Assert.assertTrue(Pattern.matches("1|20", "20"));
    }
}
